// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (C) Huawei Technologies Co., Ltd. 2022-2023. All rights reserved.
 * Description: hm pal clock functions
 * Author: Huawei OS Kernel Lab
 * Create: Mon Jul 11 19:08:16 2022
 */

#include <linux/ktime.h>
#include <linux/sched_clock.h>
#include <linux/sched/clock.h>
#include <linux/timekeeping.h>

#ifndef CONFIG_ARM64
#error only arm64 supported
#endif

#define RETRY_TOLERANCE		10
#define ADJUST_TOLERANCE	3000

static notrace u64 clock_read(void)
{
	u64 cnt;

	isb();
	cnt = read_sysreg(cntpct_el0);
	return cnt;
}

void liblinux_time_sync(void)
{
	liblinux_timekeeping_sync(RETRY_TOLERANCE, ADJUST_TOLERANCE);
}
EXPORT_SYMBOL(liblinux_time_sync);

void __init sched_clock_init(void)
{
	u64 freq = read_sysreg(cntfrq_el0);

	sched_clock_register(clock_read, 56, freq);
	generic_sched_clock_init();
}

u64 sched_clock_cpu(int cpu)
{
	return sched_clock();
}
EXPORT_SYMBOL_GPL(sched_clock_cpu);

unsigned long long liblinux_sched_clock(void)
{
	struct timespec64 ts;

	ktime_get_real_ts64(&ts);
	return ts.tv_sec * 1000000000 + ts.tv_nsec;
}

EXPORT_SYMBOL_GPL(sched_clock);

